package com.imast.android.bys.adapter;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.imast.android.bys.R;
import com.imast.android.bys.io.model.Producto;
import com.imast.android.bys.util.VolleySingleton;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ProductoAdapter extends BaseAdapter {
	
	private Context context;
    private LayoutInflater inflater;
    private List<Producto> productoItems;
    ImageLoader imageLoader;
    
    public ProductoAdapter(Context context, List<Producto> productoItems) {
		this.context = context;
		this.productoItems = productoItems;
		this.imageLoader = VolleySingleton.getInstance(context).getImageLoader();
	}
    
	@Override
	public int getCount() {
		return productoItems.size();
	}

	@Override
	public Object getItem(int position) {
		return productoItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;
		
		if (inflater == null)
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		if (convertView == null) {
			holder = new ViewHolder();
					
			convertView = inflater.inflate(R.layout.list_row_producto, null);
			holder.thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.descripcion = (TextView) convertView.findViewById(R.id.rating);
			holder.precio = (TextView) convertView.findViewById(R.id.releaseYear);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		Producto p = productoItems.get(position);
		
		holder.thumbNail.setImageUrl(p.getThumbilUrl(), imageLoader);
		holder.thumbNail.setDefaultImageResId(R.drawable.ic_holder);
		holder.title.setText(p.getTitulo());
		holder.descripcion.setText(p.getDescripcion());
		holder.precio.setText(String.valueOf(p.getPrecio()));
		
		if (imageLoader == null)
			imageLoader = VolleySingleton.getInstance(context).getImageLoader();
		
		
		return convertView;
	}
	
	public static class ViewHolder {
		NetworkImageView thumbNail;
		TextView title;
		TextView descripcion;
		TextView precio;
	}

}
